package com.bob.idea.gui.action;

import com.bob.common.DataStore;
import com.bob.common.GitPatchGenerator;
import com.bob.common.bean.PomEntity;
import com.bob.common.utils.FileUtil;
import com.bob.idea.common.Constant;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.HashSet;
import java.util.Set;


public class ButtonOkActionListener extends ButtonActionListener {
    /**
     * Invoked when an action occurs.
     *
     * @param e the event to be processed
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ButtonOkActionListener =================== ");
        final File patchPath = DataStore.getDataStore().getPatchOutPath();
        final String rootDir = DataStore.getDataStore().getRootDir();
        HashSet<String> artifactIds = (HashSet<String>) DataStore.getDataStore().getData().get(Constant.CACHE_KEY_Selectd);
        cn.hutool.core.io.FileUtil.mkdir(DataStore.getDataStore().getPatchOutPath());
        (artifactIds = (artifactIds==null?new HashSet<String>() :artifactIds)).parallelStream().forEach(artifactId->{
            PomEntity pomEntity = DataStore.getDataStore().getPomEntityMap().get(artifactId);
            String baseDir = pomEntity.getAbsolutePath();
            // TODO 路径bug 无法复制文件
            String  outputDirectory = pomEntity.getAbsolutePath()+File.separator+"target"+File.separator;
            GitPatchGenerator.copyFileToPatchDir(new File(baseDir),new File(outputDirectory),patchPath,pomEntity.getFinalName(),pomEntity.getPackaging());
        });
        GitPatchGenerator.writeChangeLogToPatchFile(patchPath);
        DataStore.getDataStore().getData().put(Constant.CACHE_KEY_openFolder, false);
        FileUtil.openFolder(patchPath);

        /*MainFrame mainFrame = (MainFrame) DataStore.getDataStore().getData().get(Constant.CACHE_KEY_frame);
        JProgressBar progressBar = new JProgressBar();
        progressBar.setStringPainted(true);
        progressBar.setString("进行中...");
        new Progress(progressBar).start();
            mainFrame.b1.add(progressBar);
        mainFrame.add(progressBar);*/
    }


    class Progress extends Thread {
        JProgressBar progressBar;
        int[] progressValues = {10, 20, 27, 39, 51, 66, 81, 100};

        public Progress(JProgressBar progressBar) {
        this.progressBar = progressBar;
        }

        @Override
        public void run() {
            for(int i=0;i<progressValues.length;i++)
            {
                try
                {
                    Thread.sleep(2000);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                //设置进度条的值
                progressBar.setValue(progressValues[i]);
            }
        }
    }
}
